﻿using System;
using System.Collections.Generic;
using System.Linq;
using Web.Models;
using Model.Core.DataBase;
using Web.Models.VersionData;


namespace Web.BML.VersionData
{
    /// <summary>
    /// 版本管理
    /// </summary>
    public class VersionOp
    {

        private static VersionOp instance;

        private VersionOp()
        {

        } 
        /// <summary>
        /// 单例模式实例化
        /// </summary>
        /// <returns></returns>
        public static VersionOp GetInstance()
        {
            if (instance == null)
            {
                instance = new VersionOp();
            }
            return instance;
        }


        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public List<VersionLib> QueryAll()
        {
            using (IDbContext db = ContainerFactory.OpenVersionConnect())
            {
                return db.Queryable<VersionLib>().ToList();
            }
        }

        /// <summary>
        /// 根据简称查询版本库
        /// </summary>
        /// <param name="shortName"></param>
        /// <returns></returns>
        public VersionLib QueryVersionLib(Guid id)
        {
            using (IDbContext db = ContainerFactory.OpenVersionConnect())
            {
                return db.Queryable<VersionLib>().FirstOrDefault(m=>m.Id==id);
            }
        }

        /// <summary>
        /// 添加版本库
        /// </summary>
        /// <param name="lib"></param>
        public Boolean AddVersionLib(VersionLib lib)
        {
            using (IDbContext db = ContainerFactory.OpenVersionConnect())
            {
                db.GetOperator<VersionLib>().Create(lib);
                return db.SaveAllChange()>0;
            }
        }
        /// <summary>
        /// 修改版本库
        /// </summary>
        /// <param name="lib"></param>
        public Boolean ModifyVersionLib(VersionLib lib)
        {
            using (IDbContext db = ContainerFactory.OpenVersionConnect())
            {
                db.GetOperator<VersionLib>().Modify(lib);
                return db.SaveAllChange() > 0;
            }
        }

        
    }


}

